package com.tuiyun.jxgl.dao;

// Generated Feb 1, 2013 4:10:46 PM by Hibernate Tools 3.4.0.CR1

import java.util.Collections;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tuiyun.jxgl.bean.Driver;
import com.tuiyun.jxgl.bean.Student;

import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class Student.
 * @see com.tuiyun.jxgl.Student
 * @author Hibernate Tools
 */
public class StudentHome extends HibernateDaoSupport{

	private static final Log log = LogFactory.getLog(StudentHome.class);
	
	public void persist(Student transientInstance) {
		log.debug("persisting Student instance");
		try {			
			this.getHibernateTemplate().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			this.getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Student instance) {
		log.debug("attaching clean Student instance");
		try {
			this.getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			this.getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Student merge(Student detachedInstance) {
		log.debug("merging Student instance");
		try {
			Student result = (Student) this.getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	public Student findByUsername(java.lang.String username) {
		log.debug("getting Student instance with id: " + username);
		try {			
			List<Student> res = this.getHibernateTemplate().findByNamedParam("from Student stu where stu.name = :username", "username", username);
			
			Student instance = null;
			if(res != null && res.size() == 1){
				instance = res.get(0);
			}
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public Student findById(java.lang.Integer id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Student instance = (Student) this.getHibernateTemplate()
					.get("com.tuiyun.jxgl.bean.Student", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Student> findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			List<Student> results = this.getHibernateTemplate().findByExample(instance);			
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public int findCountLikeNameByDriver(String key, Integer id, int state) {
		log.debug("getting Student instances size");
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "select count(distinct s.id) from com.tuiyun.jxgl.bean.Student s " +
					"where s.stuselectdriver.driver.id = ? and s.realName like ?";
			if(state != -1){
				sql += " and s.state = ?";
			}
			Query q = s.createQuery(sql);
			if(state != -1){
				q.setByte(2, (byte)state);
			}
			q.setInteger(0, id);
			q.setString(1, "%"+key+"%");
			Integer res = new Integer(String.valueOf((Long) q.uniqueResult()));
			s.close();
			return res;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Student> findByRangeLikeNameAndDriver(int page_size,
			int page_index, String key, Integer id, int state) {		
		log.debug("getting Student instances with rage_size: " + page_size + " ,rangeindex: " + page_index);
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "from com.tuiyun.jxgl.bean.Student s " +
					"where s.stuselectdriver.driver.id = ? and s.realName like ?";
			if(state != -1){
				sql += " and s.state = ?";
			}
			sql += " order by s.regdate desc";
			Query q = s.createQuery(sql);
			q.setInteger(0, id);
			q.setString(1, "%"+key+"%");
			if(state != -1){
				q.setByte(2, (byte)state);
			}
			q.setFirstResult((page_index-1)*page_size);
			q.setMaxResults(page_size);
			List<Student> ds = q.list();
			s.close();
			return ds;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			return Collections.EMPTY_LIST;
		}
	}

	public int findCountByDriver(Integer id, int state) {
		log.debug("getting Student instances size");
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "select count(distinct s.id) from com.tuiyun.jxgl.bean.Student s where s.stuselectdriver.driver.id = ?";
			if(state != -1){
				sql += " and s.state = ?";
			}
			Query q = s.createQuery(sql);
			if(state != -1){
				q.setByte(1, (byte)state);
			}
			q.setInteger(0, id);
			Integer res = new Integer(String.valueOf((Long) q.uniqueResult()));
			s.close();
			return res;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Student> findByRangeAndDriver(int page_size, int page_index,
			Integer id, int state) {		
		log.debug("getting Student instances with rage_size: " + page_size + " ,rangeindex: " + page_index);
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "from com.tuiyun.jxgl.bean.Student s where s.stuselectdriver.driver.id = ?";
			if(state != -1){
				sql += " and s.state = ?";
			}
			sql += " order by s.regdate desc";
			Query q = s.createQuery(sql);
			q.setInteger(0, id);
			if(state != -1){
				q.setByte(1, (byte)state);
			}
			q.setFirstResult((page_index-1)*page_size);
			q.setMaxResults(page_size);
			List<Student> ds = q.list();
			s.close();
			return ds;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			return Collections.EMPTY_LIST;
		}	
	}

	public int findCountLikeName(String qw, int state) {
		log.debug("getting Student instances size");
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "select count(distinct s.id) from com.tuiyun.jxgl.bean.Student s " +
					"where s.realName like ?";
			if(state != -1){
				sql += " and s.state = ?";
			}
			Query q = s.createQuery(sql);			
			q.setString(0, "%"+qw+"%");
			if(state != -1){
				q.setByte(1, (byte) state);
			}
			Integer res = new Integer(String.valueOf((Long) q.uniqueResult()));
			s.close();
			return res;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Student> findByRange(int page_size, int page_index, int state) {
		log.debug("getting Student instances with rage_size: " + page_size + " ,rangeindex: " + page_index);
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "from com.tuiyun.jxgl.bean.Student s";
			if(state != -1){
				sql += " where s.state = ?";
			}
			sql += " order by s.regdate desc";
			Query q = s.createQuery(sql);
			if(state != -1)
				q.setByte(0, (byte)state);
			q.setFirstResult((page_index-1)*page_size);
			q.setMaxResults(page_size);
			List<Student> ds = q.list();
			s.close();
			return ds;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			return Collections.EMPTY_LIST;
		}	
	}

	public int findCount(int state) {
		log.debug("getting Student instances size");
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "select count(distinct s.id) from com.tuiyun.jxgl.bean.Student s";
			if(state != -1){
				sql += " where s.state = ?";
			}
			Query q = s.createQuery(sql);
			if(state != -1){
				q.setByte(0, (byte)state);
			}
			Integer res = new Integer(String.valueOf((Long) q.uniqueResult()));
			s.close();
			return res;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Student> findByRangeLikeName(int page_size, int page_index,
			String qw, int state) {
		log.debug("getting Student instances with rage_size: " + page_size + " ,rangeindex: " + page_index);
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "from com.tuiyun.jxgl.bean.Student s " +
					"where s.realName like ?";
			if(state != -1){
				sql += " and s.state = ?";
			}
			sql += " order by s.regdate desc";
			Query q = s.createQuery(sql);			
			q.setString(0, "%"+qw+"%");
			if(state != -1){
				q.setByte(1, (byte)state);
			}
			q.setFirstResult((page_index-1)*page_size);
			q.setMaxResults(page_size);
			List<Student> ds = q.list();
			s.close();
			return ds;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			return Collections.EMPTY_LIST;
		}
	}
}
